package org.wenbo.override.core;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Page {
	String content = "";
	Session session;
	public HttpServletRequest request;
	public HttpServletResponse response;
	
	public int id=0;
	public int page=1;

	public void set(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
		session = Session.me(request);
		
		//2010-07-24
		if(param("id")!=null){
			try{
				id=Integer.parseInt(param("id"));
			}catch(java.lang.NumberFormatException e){
				
			}
			
		}
		if(param("page")!=null){
			try{
				page=Integer.parseInt(param("page"));
			}catch(java.lang.NumberFormatException e){
				
			}
		}
	}
	//2010-07-24
	public int num(String p){
		return Integer.parseInt(param(p));
	}
	
	//2010-07-23
	public String param(String s){
		if(request.getParameter(s)==null){
			return "";
		}else{
			return request.getParameter(s);
		}
	}

	public void addMode(String tag, String o) {
		content = content.replace("$" + tag, o);
	}

	public void setHTML(String file) {
		String root = request.getSession().getServletContext().getRealPath("/");
		String fileName = root + "/" + file;
		java.io.InputStream in = null;
		try {
			in = new FileInputStream(fileName);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.io.Reader re = null;
		try {
			re = new java.io.InputStreamReader(in, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		char[] chs = new char[1024];
		int count;
		try {
			while ((count = re.read(chs)) != -1) {
				content = content + new String(chs, 0, count);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void add(String tag, String o) {
		content = content.replace("$" + tag, o);
	}

	public void display() {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(content);
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* session */

	public void set(String name, Object o) {
		session.set(name, o);
	}

	public void set(Object o) {
		session.set(o);
	}

	public void remove(String name) {
		session.remove(name);
	}

	public void remove(Object o) {
		session.remove(o);
	}

	public Class<?> get(String name) {
		return session.get(name);
	}

	/* use tag and el start */

	public void addDisplay(Object o) {
		String name=o.getClass().getName().substring(o.getClass().getName().lastIndexOf("."));
		name=name.replace(".A","a");
		name=name.replace(".B","b");
		name=name.replace(".C","c");
		name=name.replace(".D","d");
		name=name.replace(".E","e");
		name=name.replace(".F","f");
		name=name.replace(".G","g");
		name=name.replace(".H","h");
		name=name.replace(".I","i");
		name=name.replace(".J","j");
		name=name.replace(".K","k");
		name=name.replace(".L","l");
		name=name.replace(".M","m");
		name=name.replace(".N","n");
		name=name.replace(".O","o");
		name=name.replace(".P","p");
		name=name.replace(".Q","q");
		name=name.replace(".R","r");
		name=name.replace(".S","s");
		name=name.replace(".T","t");
		name=name.replace(".U","u");
		name=name.replace(".V","v");
		name=name.replace(".W","w");
		name=name.replace(".X","x");
		name=name.replace(".Y","y");
		name=name.replace(".Z","z");
		request.setAttribute(name, o);
	}

	public void addDisplay(String name, Object o) {
		request.setAttribute(name, o);
	}

	public void display(String filename) {
		try {
			request.getRequestDispatcher(filename).forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void echo(String text) {
		// response.reset();
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(text);
	}
	public void echo(int i) {
		//2010-07-26 增加了数字输出
		// response.reset();
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(i);
	}
	/* use tag and el end */
}
